//
//  Loading.m
//  礼物说等待动画
//
//  Created by QQn1993 on 16/1/12.
//  Copyright © 2016年 QQn1993. All rights reserved.
//

#import "Loading.h"

@interface Loading ()

@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,strong) UIImageView *img;

@end

@implementation Loading

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.5];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = self.frame.size.width / 6;
        self.img = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.img.image = [UIImage imageNamed:@"loading_02"];
        [self addSubview:self.img];
        
        self.array = [NSMutableArray arrayWithCapacity:0];
        for (NSInteger i = 2; i < 21; i ++) {
            if (i < 9 && i != 5) {
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0%ld",i]];
                [self.array addObject:img];
            }
            
            else if (i > 9 && i != 13 && i != 17){
                UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"loading_%ld",i]];
                [self.array addObject:img];
            }
            
        }
        
    }
    return self;
}

- (void)start
{
    self.img.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
    [self.img setAnimationImages:self.array];
    [self.img setAnimationDuration:4];
    [self.img startAnimating];
    
}

- (void)stop
{
    [self.img stopAnimating];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"jiazaidengdai" object:[NSString stringWithFormat:@"滚回来"]];
        
    });
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
